package com.wx;

import lombok.extern.slf4j.Slf4j;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

@Slf4j
public class SpElTest {
    /**
     * Spel表达式解析器
     */
    static SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
    public static void main(String[] args) {


        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariable("person", new Person("wx", 11));
        String expression = "Hello {#person.age}";
        Expression parseExpression = spelExpressionParser.parseExpression(expression,new CommonParserContext());
        Object value =  parseExpression.getValue(context);
        System.out.println(value);

    }


    /**
     * 自定义解析器上下文环境
     *
     * @author wangxun
     */
    public static class CommonParserContext implements ParserContext {
        @Override
        public boolean isTemplate() {
            return true;
        }

        @Override
        public String getExpressionPrefix() {
            return "{";
        }

        @Override
        public String getExpressionSuffix() {
            return "}";
        }
    }
}
